<template>
  <div class="usage-detail-container">
    <!-- 筛选区域 -->
    <div class="filter-section">
      <div class="filter-item">
        <span class="filter-label">类型:</span>
                 <el-select v-model="filterForm.type" placeholder="请选择类型" style="width: 100px;">
          <el-option label="扣费" value="1"></el-option>
          <el-option label="充值" value="2"></el-option>
        </el-select>
      </div>

      <div class="filter-item">
        <span class="filter-label">扣费时间:</span>
                 <el-date-picker
           v-model="filterForm.starttime"
           type="date"
           placeholder="开始日期"
           format="YYYY-MM-DD"
           value-format="YYYY-MM-DD"
           style="width: 110px;"
         />
         <span class="date-separator">至</span>
         <el-date-picker
           v-model="filterForm.endtime"
           type="date"
           placeholder="结束日期"
           format="YYYY-MM-DD"
           value-format="YYYY-MM-DD"
           style="width: 110px;"
         />
      </div>

      <el-button type="primary" @click="handleQuery" :loading="loading" size="default" class="query-btn">查询</el-button>
    </div>

    <!-- 数据表格 -->
    <div class="table-section">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        stripe
        border
        :empty-text="loading ? '加载中...' : '暂无数据'"
                 :max-height="333"
        class="custom-table"
      >
        <el-table-column prop="desc" label="类型" width="140">
          <template #default="scope">
            {{ getTypeDisplayName(filterForm.type) }}
          </template>
        </el-table-column>
                 <el-table-column prop="task_name" label="任务名" width="200">

         </el-table-column>
         <el-table-column prop="task_count" label="数量" width="80">

         </el-table-column>
         <el-table-column prop="price" label="点数" width="100">

         </el-table-column>
        <el-table-column prop="addtime" label="时间" width="240" />
        <el-table-column prop="remark" label="备注" min-width="120" />
             </el-table>

       <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handlePageSizeChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue'
import { Close } from '@element-plus/icons-vue'
import axiosUtil from "../../axios-util/axios-util.js"

// 响应式数据
const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const showSummary = ref(true)

// 筛选表单
const filterForm = reactive({
  type: '1',
  starttime: '',
  endtime: ''
})

// 统计数据
const summaryData = ref({
  video: 0,
  collage: 0,
  imageText: 0,
  digitalHuman: 0
})

// 计算属性
const paginationParams = computed(() => ({
  offset: currentPage.value,
  limit: pageSize.value,
  starttime: filterForm.starttime || '',
  endtime: filterForm.endtime || '',
  type: filterForm.type
}))

// 获取使用详情数据
const fetchUsageData = async () => {
  loading.value = true
  try {
    const response = await axiosUtil.post('api/store/pay', paginationParams.value)
    if (response.data.code === 1) {
      tableData.value = response.data.data.rows || []
      total.value = response.data.data.total || 0
      calculateSummary()
    } else {
      console.error('获取使用详情失败:', response.data.msg)
    }
  } catch (error) {
    console.error('请求使用详情出错:', error)
  } finally {
    loading.value = false
  }
}

// 计算统计数据
const calculateSummary = () => {
  const summary = {
    video: 0,
    collage: 0,
    imageText: 0,
    digitalHuman: 0
  }

  tableData.value.forEach(item => {
    if (item.remark === '一键成片') {
      summary.video += parseInt(item.task_count) || 0
    } else if (item.remark === '拼图') {
      summary.collage += parseInt(item.task_count) || 0
    } else if (item.remark === '图文') {
      summary.imageText += parseInt(item.task_count) || 0
    } else if (item.remark === '数字人') {
      summary.digitalHuman += parseInt(item.task_count) || 0
    }
  })

  summaryData.value = summary
}

// 查询按钮点击
const handleQuery = () => {
  currentPage.value = 1
  fetchUsageData()
}

// 页码改变
const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchUsageData()
}

// 每页条数改变
const handlePageSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
  fetchUsageData()
}

// 关闭统计面板
const closeSummary = () => {
  showSummary.value = false
}

// 获取类型显示名称
const getTypeDisplayName = (desc) => {
    return desc==='1'?'扣费':'充值'
}

// 组件挂载时获取数据
onMounted(() => {
  fetchUsageData()
})
</script>

<style scoped>
.usage-detail-container {
  padding: 16px;
}

.filter-section {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  flex-wrap: wrap;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 180px;
}

.filter-label {
  font-weight: 500;
  color: #333;
  white-space: nowrap;
  font-size: 14px;
}

.date-separator {
  color: #666;
  margin: 0 8px;
  font-weight: 500;
}

.query-btn {
  min-width: 70px;
  height: 32px;
  border-radius: 6px;
  font-weight: 500;
}

.table-section {
  margin-bottom: 16px;
}

.price-text {
  color: #e6a23c;
  font-weight: 500;
  font-size: 14px;
}

.pagination-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  margin-top: 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
}

.pagination-info {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

/* 表格样式 - 与系统其他表格保持一致 */
.custom-table {
  background: transparent !important;
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  border-radius: 8px !important;
  overflow: hidden;
}

.custom-table :deep(.el-table__header) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.custom-table :deep(.el-table__header th) {
  background: rgba(255, 255, 255, 0.1) !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2) !important;
  color: #333 !important;
  font-weight: bold !important;
}

.custom-table :deep(.el-table__header-wrapper) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.custom-table :deep(.el-table__header tr) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.custom-table :deep(.el-table__body-wrapper) {
  background: transparent !important;
}

.custom-table :deep(.el-table__body td) {
  background: rgba(255, 255, 255, 0.2) !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
  color: #333 !important;
}

.custom-table :deep(.el-table__row) {
  background: rgba(255, 255, 255, 0.2) !important;
  transition: all 0.3s ease !important;
}

.custom-table :deep(.el-table__row:hover) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.custom-table :deep(.el-table__row:hover td) {
  background: rgba(255, 255, 255, 0.1) !important;
}

.custom-table :deep(.el-table__cell) {
  background: transparent !important;
}

.custom-table :deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  background: rgba(255, 255, 255, 0.15) !important;
}

.custom-table :deep(.el-table--striped .el-table__body tr.el-table__row--striped:hover td) {
  background: rgba(255, 255, 255, 0.1) !important;
}

/* 分页样式 */
:deep(.el-pagination) {
  justify-content: flex-end;
  margin-top: 12px;
  padding: 8px 0;
}

:deep(.el-pagination .el-pager li) {
  border-radius: 4px;
  margin: 0 2px;
}

:deep(.el-pagination .el-pager li.is-active) {
  background-color: var(--el-color-primary);
  color: white;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .filter-section {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-item {
    min-width: auto;
    width: 100%;
  }

  .pagination-section {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
}
</style>
